# \6_17
# test.py
# test.py
import time
from time import sleep

from selenium.webdriver import Chrome, ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


def t1():
    driver = Chrome()
    driver.get("D:/github/python/6_19/1.html")

    # 定位input标签并选择 Python 和 C++
    checkbox_names_to_select = ["Python", "C++"]
    tag_input = driver.find_elements(By.TAG_NAME, 'input')  # 获取所有input标签
    for checkbox in tag_input:  # 遍历input标签
        if checkbox.get_attribute("type") == "checkbox":  # 判断标签类型是否为checkbox
            checkbox_name = checkbox.get_attribute("name")  # 获取标签的name属性
            if checkbox_name in checkbox_names_to_select:  # 判断标签的name属性是否在checkbox_names_to_select中
                checkbox.click()  # 点击标签
                time.sleep(2)

    time.sleep(5)
    driver.refresh()  # 刷新页面
    time.sleep(5)

    # 再次定位 input 标签
    tag_input = driver.find_elements(By.TAG_NAME, 'input')  # 重新查找元素
    for type_value in tag_input:  # 遍历所有 input 标签
        print(type_value.get_attribute("name"))  # 获取标签的 name 属性
        if type_value.get_attribute("type") == "checkbox":  # 判断标签的 type 属性是否为 checkbox
            type_value.click()  # 点击标签
            time.sleep(2)

    time.sleep(3)
    driver.quit()


def t2():
    driver = Chrome()
    driver.get("D:/github/python/6_19/2.html")
    driver.maximize_window()
    # 定位下拉列表标签,并创建下拉列表对象
    select = Select(driver.find_element(By.TAG_NAME, "select"))
    # 通过value属性选择选项
    select.select_by_value("Python")
    time.sleep(2)
    # 通过⽂本内容选择选项
    select.select_by_visible_text("C++")
    time.sleep(2)
    # 通过选项索引号选择选项
    select.select_by_index(0)  # 等同于 select.options[0].click()
    time.sleep(2)
    # 通过options属性循环选取
    for i in select.options:
        i.click()
        time.sleep(2)
    # 关闭浏览器
    time.sleep(3)
    driver.quit()


def t3():
    driver = Chrome()
    driver.get("D:/github/python/6_19/3.html")
    driver.maximize_window()

    # alert()弹窗处理
    driver.find_element(By.ID, "alert").click()
    sleep(2)
    # 获取弹窗对象
    alert = driver.switch_to.alert
    # 输出弹窗内容
    print(alert.text)  # 结果：这是alert()弹窗!
    # 确认弹窗
    alert.accept()
    sleep(2)

    # confirm()弹窗
    driver.find_element(By.ID, "confirm").click()
    sleep(2)
    # 获取弹窗对象
    confirm = driver.switch_to.alert
    # 输出弹窗内容
    print(confirm.text)  # 这是confirm()弹窗！
    # 执⾏性弹窗的取消按钮
    confirm.dismiss()  # confirm.accept() # 执⾏弹窗确认按钮
    sleep(2)
    # prompt()弹窗
    driver.find_element(By.ID, "prompt").click()
    sleep(2)

    # 获取弹窗对象
    prompt = driver.switch_to.alert
    # 输出弹窗内容
    print(prompt.text)  # 这是prompt()弹窗！
    # 向弹窗的输⼊框内输⼊内容
    prompt.send_keys("这是弹窗prompt()")
    sleep(2)
    # 执⾏性弹窗的确认按钮
    prompt.accept()
    # 关闭浏览器
    sleep(3)
    driver.quit()


def t4():
    driver = Chrome()
    driver.maximize_window()
    driver.get("D:/github/python/6_19/4.html")

    cwd = driver.current_window_handle  # 获取当前窗口句柄
    wait = WebDriverWait(driver, 20)

    # 切换到 iframe
    frame = wait.until(EC.presence_of_element_located((By.NAME, "navIframe2")))  # 获取iframe元素
    driver.switch_to.frame(frame)  # 切换到iframe

    time.sleep(2)

    # 输入篮球
    search_input = wait.until(EC.presence_of_element_located((By.XPATH, '//input[@id="q"]')))
    search_input.send_keys("篮球")

    # 等待搜索按钮可见并点击
    # search_button = wait.until(
    #     EC.element_to_be_clickable((By.XPATH, '//*[@id="J_TSearchForm"]/div[2]/button'))
    # )
    # search_button.click()

    # time.sleep(10)
    # driver.back()  # 返回上一页
    # time.sleep(3)

    # 返回主页面
    driver.switch_to.parent_frame()

    # 点击苏宁商城，进⼊苏宁商城⻚⾯
    # 切换到 iframe
    frame = wait.until(EC.presence_of_element_located((By.NAME, "navIframe3")))  # 获取iframe元素
    driver.switch_to.frame(frame)  # 切换到iframe
    time.sleep(5)
    search_input = driver.find_element(By.ID, "searchKeywords")
    search_input.send_keys("手机")
    # 再返回初始⻚⾯
    time.sleep(5)
    driver.switch_to.window(cwd)
    time.sleep(5)
    # 最后，关闭浏览器驱动
    driver.quit()


def t5():
    # 鼠标悬停
    driver = Chrome()
    driver.get("https://baidu.com")
    sleep(5)
    action = ActionChains(driver)
    tag = driver.find_element(By.CLASS_NAME, "mnav.s-top-more-btn")
    action.move_to_element(tag).perform()
    sleep(5)


def t6():
    driver = Chrome()
    driver.get("https://baidu.com")
    sleep(3)
    title = driver.title
    print(title)


# if __name__ == '__main__':
#     # 获取标签元素的属性值（复选框操作）
#     t1()

# if __name__ == '__main__':
#     # 下拉列表操作
#     t2()

# if __name__ == '__main__':
#     # 弹窗操作
#     t3()
if __name__ == '__main__':
    # frame/iframe内元素操作
    t4()
# if __name__ == '__main__':
#     # 鼠标移动操作
#     t5()
# if __name__ == '__main__':
#     #  获取页面标题
#     t6()
